/*
 * twist.cpp
 *
 * Copyright 2012 Maximilian Eisbach <max@itp.tu-berlin.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */

#include <mylibs/CardiacMesh.hpp>
#include <mylibs/myinifiles.hpp>
#include <iostream>
#include <fstream>
#include <sys/stat.h> 	// mkdir

using namespace std;

bool verbose = false;

mystring output = mystring();

int main(int argc, char **argv){
	myIniFiles ini(argc, argv);

	ini.set_info("Convert data given on intracellular nodes to data\
 given on the extracellular mesh by adding 0. at extracellular nodes.");

	ini.register_param("mesh", "m", "Mesh basename without extension.");
	ini.register_param("data", "d", "Filename of the datfile.");
	ini.check();

	mystring meshfile;
	mystring datafile;

	meshfile = ini.read("mesh", mystring());
	datafile = ini.read("data", mystring());

	if (meshfile.empty())
		ini.print_help("mesh");

	//load the standard input mesh
	CardiacMesh mesh(meshfile, true);

	// Read in fiber structure
	mesh.read_lon();
//	// read in transmural position
//	mesh.read_transmural_pos();

	size_t lines = (size_t) -1;
	{
		double dummy = 0.;
		ifstream f(datafile.c_str());
		do {
			f >> dummy;
			lines++;
		} while (f.good());
		f.close();
	}

	cout << "lines : " << lines << endl;

	double *data = new double[lines];
	ifstream f(datafile.c_str());
	for (size_t i = 0; i < lines; i++)	{
		f >> data[i];
		if (f.fail()) {
			cerr << datafile << " : I/O error." << endl;
			delete[] data;
			return -1;
		}
	}
	f.close();

	// convert i2e
	if (lines == mesh.intra_elements()){
		cout << "Converting file intra2extra" << endl;
		mystring datafout(datafile);
		datafout.replace("_i.", "_e.");

		double *out = new double[mesh.extra_elements()];

		// init out array
		for (size_t i = 0; i < mesh.extra_elements(); i++){
			out[i] = 0.;
		}

		for (size_t i = 0; i < mesh.intra_elements(); i++){
			const size_t &i2e = mesh.elem_i2e(i);
			out[i2e] = data[i];
		}

		cout << " - Writing " << datafout << endl;
		ofstream f(datafout.c_str());
		for (size_t i = 0; i < mesh.extra_elements(); i++){
			f << out[i] << endl;
			if (f.fail()) {
				cerr << "out.dat" << " : I/O error." << endl;
				return -1;
			}
		}
		f.close();
		delete[] out;
	} else if (lines == mesh.extra_elements()){
		cout << datafile << " stores data on extra elements." << endl;
	} else if (lines == mesh.points()){
		cout << datafile << " stores data on extra nodes." << endl;
	}


	delete[] data;




//	for (size_t i = 0; i < mesh.intra_elements(); i++){
//		const size_t &idx = mesh.elem_i2e(i);
//		if (mesh.transmural_position(i) > 0.7)
//				mesh.f[idx].attribute = 30;
//		else
//				mesh.f[idx].attribute = 0;
//	}

//	output = "test";
//	mesh.save_mesh(output.c_str());

	return 0;
}
